package com.iptv.grape.ui.dlg;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.iptv.grape.R;
import com.iptv.grape.util.ScreenUtils;

public class DlgMgr {
    private static Dialog mDlg ;

    public static void cancel(){
        if(null!= mDlg && mDlg.isShowing()){
            mDlg.dismiss();
        }
        mDlg = null;
    }


    public static void cfmDlg(Context ctx,final String value,final Click click){
        cancel();
        mDlg = new CstmDlg.Builder(ctx)
                .width((int)(ScreenUtils.getScreenWidth(ctx)*0.5))
                .height((int)(ScreenUtils.getScreenHeight(ctx)*0.4))
                .listener(new CstmListener() {
                    @Override
                    public void initView(Context ctx, View parentView, View.OnClickListener listener) {
                        TextView tvContent = parentView.findViewById(R.id.tvContent);
                        tvContent.setText(value);
                        parentView.findViewById(R.id.btnCancel).setOnClickListener(listener);
                        parentView.findViewById(R.id.btnOk).setOnClickListener(listener);
                    }
                    @Override
                    public void dismiss() {
                        cancel();
                    }

                    @Override
                    public void click(View view) {
                        super.click(view);
                        click.onListener(view);
                        dismiss();
                    }
                }).build();
        mDlg.show();
    }
    public static void apkTreatDlg(Context ctx, final Drawable icon, final Click click){
        cancel();
        mDlg = new CstmDlg.Builder(ctx)
                .layout(R.layout.dlg_apktreat)
                .dlgBg(R.drawable.dlg_apktreat_bg)
                .width((int)(ScreenUtils.getScreenWidth(ctx)*0.5))
                .height((int)(ScreenUtils.getScreenHeight(ctx)*0.4))
                .listener(new CstmListener() {
                    @Override
                    public void initView(Context ctx, View parentView, View.OnClickListener listener) {
                        ImageView ivIcon = parentView.findViewById(R.id.ivIcon);
                        ivIcon.setImageDrawable(icon);
                        parentView.findViewById(R.id.btnOpenApk).setOnClickListener(listener);
                        parentView.findViewById(R.id.btnDelApk).setOnClickListener(listener);
                    }
                    @Override
                    public void dismiss() {
                        cancel();
                    }

                    @Override
                    public void click(View view) {
                        super.click(view);
                        click.onListener(view);
                        dismiss();
                    }
                }).build();
        mDlg.show();
    }



    public static void grapeInitDlg(Context ctx, final String value){
        mDlg = new CstmDlg.Builder(ctx)
                .layout(R.layout.dlg_grapeinit)
                .width((int)(ScreenUtils.getScreenWidth(ctx)*0.5))
                .height((int)(ScreenUtils.getScreenHeight(ctx)*0.4))
                .listener(new CstmListener() {
                    @Override
                    public void initView(Context ctx, View parentView, View.OnClickListener listener) {
                        TextView tvContent = parentView.findViewById(R.id.tvMsg);
                        tvContent.setText(value);
                    }
                    @Override
                    public void dismiss() {
                        cancel();
                    }

                }).build();
        mDlg.show();
    }


    public interface Click{
        void onListener(View view);
    }



}
